package com.changjiang.myproductsale.myproduct.servlet;

import com.changjiang.myproductsale.myproduct.dao.impl.CategoryImpl;
import com.changjiang.myproductsale.myproduct.dao.impl.ProductImpl;
import com.changjiang.myproductsale.myproduct.entity.Category;
import com.changjiang.myproductsale.myproduct.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ServletIndex", value = "/index")
public class ServletIndex extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取热销商品信息
            ProductImpl productDao = new ProductImpl();
            List<Product> HotProducts = productDao.getHotProducts();
            request.setAttribute("HotProducts", HotProducts);

            // 获取搜索量前16名的商品信息
            List<Product> topSearchProducts = productDao.getTopSearchProducts();
            request.setAttribute("topSearchProducts", topSearchProducts);

            // 获取所有分类信息
            CategoryImpl categoryDao = new CategoryImpl();
            List<Category> categories = categoryDao.getAllCategories();
            request.setAttribute("categories", categories);

            // 转发请求到index.jsp页面
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
            response.sendRedirect("500.html");
        }
    }
}